package com.arcvideo.acsplayer.widget;

import android.content.Context;
import android.content.res.Configuration;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.arcvideo.acsplayer.R;
import com.arcvideo.acsplayer.utils.DateUtil;

public class FastMoveView extends FrameLayout {

	public static final int STATE_FORWORD = 1;
	public static final int STATE_BACKWORD = 2;

	private Context mContext = null;
	// private ImageView mFastMoveImage = null;
	private ImageView mFastMoveLeftImage = null;
	private ImageView mFastMoveRightImage = null;
	private ImageButton mControlGyroButton = null;
	private Button mControlHDButton = null;
	private TextView mLeftTimeText = null;
	private TextView mRightTimeText = null;

	private int mCurrentState = STATE_FORWORD;
	private int mRotationType = 0;
	private boolean mbVRMode = false;
	private int mCurrentPostion = 0;
	private int mDuration = 0;

	private FastMoveInterface mFastMoveInterface = null;

	/**
	 * 回调接口
	 *
	 * @author skl5250
	 *
	 */
	public interface FastMoveInterface {
		public void clickControlGyro();

		public void clickControlHD();

		public void clickControlFastFroward();

		public void clickControlFastBackward();
	}

	/**
	 * 按键事件
	 */
	private OnClickListener mOnClickListener = new OnClickListener() {

		@Override
		public void onClick(View view) {
			switch (view.getId()) {
				case R.id.control_gyro :
					if (mbVRMode) {
						if (null != mFastMoveInterface)
							mFastMoveInterface.clickControlGyro();
					}
					setVisibility(View.GONE);
					break;
				case R.id.control_hd :
					if (mbVRMode) {
						if (null != mFastMoveInterface)
							mFastMoveInterface.clickControlHD();
					}
					setVisibility(View.GONE);
					break;
				case R.id.fastmove_left :
					if (mbVRMode) {
						if (null != mFastMoveInterface)
							mFastMoveInterface.clickControlFastBackward();
					}
					break;
				case R.id.fastmove_right :
					if (mbVRMode) {
						if (null != mFastMoveInterface)
							mFastMoveInterface.clickControlFastFroward();
					}
					break;
				default :
					break;
			}

		}
	};

	/**
	 * 构造函数
	 *
	 * @param context
	 */
	public FastMoveView(Context context) {
		super(context);
		mContext = context;
	}

	/**
	 * 构造函数
	 *
	 * @param context
	 * @param attributeSet
	 */
	public FastMoveView(Context context, AttributeSet attributeSet) {
		super(context, attributeSet);
		mContext = context;
	}

	/**
	 * 初始化
	 *
	 * @param rotationType
	 * @param state
	 * @param isVRMode
	 * @param fastMoveInterface
	 */
	public void init(int rotationType, int state, boolean isVRMode, FastMoveInterface fastMoveInterface) {
		mRotationType = rotationType;
		mCurrentState = state;
		mbVRMode = isVRMode;
		mFastMoveInterface = fastMoveInterface;
		initUI();
	}

	/**
	 * 初始化UI
	 */
	public void initUI() {
		LayoutInflater.from(mContext).inflate(R.layout.actionbar_fastmove_all, this);
		mFastMoveLeftImage = (ImageView) findViewById(R.id.fastmove_left);
		mFastMoveLeftImage.setOnClickListener(mOnClickListener);
		mFastMoveRightImage = (ImageView) findViewById(R.id.fastmove_right);
		mFastMoveRightImage.setOnClickListener(mOnClickListener);
		mControlGyroButton = (ImageButton) findViewById(R.id.control_gyro);
		mControlGyroButton.setOnClickListener(mOnClickListener);
		mControlHDButton = (Button) findViewById(R.id.control_hd);
		mControlHDButton.setOnClickListener(mOnClickListener);
		mLeftTimeText = (TextView) findViewById(R.id.fastmove_time_left);
		mRightTimeText = (TextView) findViewById(R.id.fastmove_time_right);

		reload(mRotationType, mbVRMode);
		update(mCurrentState);
	}

	/**
	 * 重新加載
	 *
	 * @param rotationType
	 * @param isVRMode
	 */
	public void reload(int rotationType, boolean isVRMode) {
		mRotationType = rotationType;
		mbVRMode = isVRMode;

		if (mbVRMode) {
			mFastMoveLeftImage.setVisibility(VISIBLE);
			mFastMoveRightImage.setVisibility(VISIBLE);
			if (mRotationType == Configuration.ORIENTATION_LANDSCAPE) {
				mControlGyroButton.setVisibility(GONE);
				mControlHDButton.setVisibility(GONE);
			} else if (mRotationType == Configuration.ORIENTATION_PORTRAIT) {
				mControlGyroButton.setVisibility(VISIBLE);
				mControlHDButton.setVisibility(VISIBLE);
			}
		} else {
			mFastMoveRightImage.setVisibility(GONE);
			mControlGyroButton.setVisibility(GONE);
			mControlHDButton.setVisibility(GONE);
		}
	}

	/**
	 * 设置进度
	 *
	 * @param currentPostion
	 * @param duration
	 */
	public void setPostion(int currentPostion, int duration) {
		mCurrentPostion = currentPostion;
		mDuration = duration;
		mLeftTimeText.setText(DateUtil.getTimeCount(mCurrentPostion));
		mRightTimeText.setText(DateUtil.getTimeCount(mDuration));
	}

	/**
	 * 更新当前状态
	 *
	 * @param state
	 */
	public void update(int state) {
		mCurrentState = state;

		if (mbVRMode) {
			if (mRotationType == Configuration.ORIENTATION_LANDSCAPE) {
				mFastMoveRightImage.setBackgroundResource(R.drawable.btn_fast_forward_h);
				mFastMoveLeftImage.setBackgroundResource(R.drawable.btn_rewind_h);
			} else if (mRotationType == Configuration.ORIENTATION_PORTRAIT) {
				mFastMoveRightImage.setBackgroundResource(R.drawable.btn_fast_forward_v);
				mFastMoveLeftImage.setBackgroundResource(R.drawable.btn_rewind_v);
			}
		} else {
			if (mCurrentState == STATE_FORWORD) {
				mFastMoveLeftImage.setBackgroundResource(R.drawable.btn_fast_forward_p);
			} else if (mCurrentState == STATE_BACKWORD) {
				mFastMoveLeftImage.setBackgroundResource(R.drawable.btn_rewind_p);
			}
		}
	}

	/**
	 * 恢复初始设置
	 */
	private void reset() {
		if (mbVRMode) {
			if (mRotationType == Configuration.ORIENTATION_LANDSCAPE) {
				mFastMoveRightImage.setBackgroundResource(R.drawable.btn_fast_forward_h);
				mFastMoveLeftImage.setBackgroundResource(R.drawable.btn_rewind_h);
			} else if (mRotationType == Configuration.ORIENTATION_PORTRAIT) {
				mFastMoveRightImage.setBackgroundResource(R.drawable.btn_fast_forward_v);
				mFastMoveLeftImage.setBackgroundResource(R.drawable.btn_rewind_v);
			}
		} else {
			if (mCurrentState == STATE_FORWORD) {
				mFastMoveLeftImage.setBackgroundResource(R.drawable.btn_fast_forward);
			} else if (mCurrentState == STATE_BACKWORD) {
				mFastMoveLeftImage.setBackgroundResource(R.drawable.btn_rewind);
			}
		}
	}

	/**
	 * 显示
	 */
	public void show() {
		setVisibility(View.VISIBLE);
	}

	/**
	 * 隐藏
	 */
	public void hide() {
		reset();
		setVisibility(View.INVISIBLE);
	}

	/**
	 * 设置画质状态
	 *
	 * @param state
	 */
	public void setQualityState(String state) {
		if (HDselectView.STATE_4K.equals(state))
			mControlHDButton.setText(R.string.clarity_4K);
		else if (HDselectView.STATE_ULTRA_CLEAR.equals(state))
			mControlHDButton.setText(R.string.clarity_ultra_clear);
		else if (HDselectView.STATE_HD.equals(state))
			mControlHDButton.setText(R.string.clarity_hd);
		else if (HDselectView.STATE_SD.equals(state))
			mControlHDButton.setText(R.string.clarity_sd);
		else if (HDselectView.STATE_SMOOTH.equals(state))
			mControlHDButton.setText(R.string.clarity_smooth);
	}

	/**
	 * 设置陀螺仪状态
	 *
	 * @param isOn
	 */
	public void setGyroState(boolean isOn) {
		if (isOn) {
			mControlGyroButton.setBackgroundResource(R.drawable.btn_gyro_p);
		} else {
			mControlGyroButton.setBackgroundResource(R.drawable.btn_gyro_n);
		}
	}

}